#include "FileControl.h"
#include "Editor.h"
#include "Basic/FileSystem.h"

namespace FinalRendering
{
	namespace Editor
	{
		FileControl::FileControl(GxWinGUI::Form *parent, GxRendering::UI::UIEntry * _ui)
		{
			form = parent;
			ui = _ui;
			dlgOpen = new FileDialog((GxWinGUI::Component *)form);
			dlgSave = new FileDialog((GxWinGUI::Component *)form);
			dlgOpen->DefaultEXT = L"gxmap";
			dlgOpen->Filter = L"FinalRendering Maps|*.gxmap";
			dlgOpen->FileMustExist = true;
			dlgOpen->PathMustExist = true;
			dlgSave->DefaultEXT = dlgOpen->DefaultEXT;
			dlgSave->Filter = dlgOpen->Filter;
			dlgSave->FileMustExist = false;
			dlgSave->PathMustExist = true;
			dlgSave->OverwritePrompt = true;

		}

		String FileControl::GetCurrentName()
		{
			String rs = WorldEditor->Scene->FileName;
			if (rs == L"")
			{
				return L"Untitiled.gxmap";
			}
			else
				return ExtractFileName(rs);
		}

		bool FileControl::Close()
		{
			if (WorldEditor->Scene->Modified)
			{
				String msg = L"Save changes to \"";
				msg += GetCurrentName();
				msg += L"\" ?";
				int rs = MessageBox(form->GetHandle(), msg.GetStrPtr(), L"World Editor", MB_ICONEXCLAMATION | MB_YESNOCANCEL);
				if (rs == IDYES)
				{
					if (!Save())
						return false;
				}
				else if (rs == IDCANCEL)
				{
					return false;
				}
			}
			WorldEditor->NewDoc();
			WorldEditor->Views->UpdateAllViews();
			return true;
		}

		bool FileControl::Open()
		{
			if (dlgOpen->ShowOpen())
			{
				if (!Close())
					return false;
				try
				{
					WorldEditor->Scene->LoadFromFile(dlgOpen->FileName);
					WorldEditor->Views->UpdateAllViews();
					return true;
				}
				catch (gxFileSystemException)
				{
					form->MessageBox(String(L"Cannot open \"")+dlgOpen->FileName + L"\".", L"World Editor", MB_ICONEXCLAMATION);
					return false;
				}
				catch (int v)
				{
					if (v == 0)
						form->MessageBox(String(L"Cannot open \"")+dlgOpen->FileName + L"\", file has been damaged", L"World Editor", MB_ICONEXCLAMATION);
					else if (v == 1)
					{
						form->MessageBox(String(L"Cannot open \"")+dlgOpen->FileName + L"\", this file is created by a newer version of World Editor.", L"World Editor", MB_ICONEXCLAMATION);
					}
					return false;
				}
			}
			else
				return false;
		}

		bool FileControl::Save()
		{
			if (FileExists(WorldEditor->Scene->FileName))
			{
				try
				{
					WorldEditor->Scene->SaveToFile(WorldEditor->Scene->FileName);
					return true;
				}
				catch (...)
				{
					form->MessageBox(String(L"Cannot save to \"")+WorldEditor->Scene->FileName + L"\", please choose another location.",
						L"World Editor", MB_ICONEXCLAMATION);
				}
			}
			return SaveAs();
		}

		bool FileControl::SaveAs()
		{
			while (1)
			{
				if (!dlgSave->ShowSave())
					return false;
				try
				{
					WorldEditor->Scene->SaveToFile(dlgSave->FileName);
					return true;
				}
				catch (gxFileSystemException)
				{
					form->MessageBox(String(L"Cannot save to \"")+WorldEditor->Scene->FileName + L"\", please choose another location.",
						L"World Editor", MB_ICONEXCLAMATION);
				}
				catch (...)
				{
					break;
				}
			}
		}

		bool FileControl::New()
		{
			return Close();
		}


	}
}